<template>

    <el-form label-width="100px" label-position="left">

        <el-form-item label="名称">

            <el-input v-model="customer_group.name" > </el-input>

        </el-form-item>

        <el-form-item label="描述">

            <el-input v-model="customer_group.description" > </el-input>
        </el-form-item>

        <el-form-item label="排序">
            <el-input-number min="0" v-model="customer_group.sort_order"> </el-input-number>
        </el-form-item>

        <el-form-item label="默认">
            <el-switch v-model="customer_group.is_default" 
            active-value="1"
            inactive-value="0"/>
        </el-form-item>

        <el-divider></el-divider>
        <el-form-item label="">
            <el-button type="primary" @click="do_edit()" >保存</el-button>
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>

import { ref } from "vue";
import { useRoute,useRouter } from "vue-router";
import axios from "axios";
import { data } from "dom7";
import { ElMessage } from "element-plus";
const VITE_URL = window.VITE_URL;

const route = useRoute();
const router = useRouter();
// let group_id:number = 0;
let group_id:any = 0;

const customer_group:any = ref<object>({
    sort_order:0,
    is_default:0
})
const get_customer_group = ()=>{

    const url = VITE_URL + '/admin.customerGroup/single?id=' + group_id;
    axios.get( url ).then( res => {

        customer_group.value = res.data
        customer_group.value.is_default += '';
    } )

}

const do_edit = () => {

    const url = VITE_URL + '/admin.customerGroup/edit?id=' + group_id;
    
    const data = {
        name        : customer_group.value.name,
        description : customer_group.value.description,
        sort_order  : customer_group.value.sort_order,
        is_default  : customer_group.value.is_default,
    }
    axios.post( url, data ).then( res => {

        ElMessage( {
                message: !res.data.status?'保存成功':res.data.message,
                duration: 1000,
                type:!res.data.status?'success':'error',
                'on-close':function(){
                    if( !res.data.status ){
                        router.push({path:'/main/customer/group/list'});
                    }
                }
            } )


    } )

}

const init = ()=>{


    group_id = route.params.id?route.params.id:0;

    if( group_id ){

        get_customer_group();
    }
}

init();

</script>